package com.zyf.lj.controller;

import com.zyf.lj.entity.JobApplication;
import com.zyf.lj.service.JobApplicationService;
import com.zyf.lj.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 职位申请控制器
 */
@RestController
@RequestMapping("/api/employment")
public class JobApplicationController {
    
    @Autowired
    private JobApplicationService jobApplicationService;
    
    @Value("${file.upload}")
    private String uploadPath;
    
    /**
     * 提交职位申请
     * @param jobId 职位ID
     * @param name 申请人姓名
     * @param phone 手机号
     * @param email 电子邮箱
     * @param introduction 个人简介
     * @param resumeFile 简历文件
     * @return 提交结果
     */
    @PostMapping("/apply")
    public Result<Void> submitApplication(
            @RequestParam Integer jobId,
            @RequestParam String name,
            @RequestParam String phone,
            @RequestParam(required = false) String email,
            @RequestParam String introduction,
            @RequestParam(required = false) MultipartFile resumeFile) {
        
        // 表单验证
        if (name == null || name.trim().isEmpty()) {
            return Result.fail("姓名不能为空");
        }
        if (phone == null || phone.trim().isEmpty()) {
            return Result.fail("手机号不能为空");
        }
        if (introduction == null || introduction.trim().isEmpty()) {
            return Result.fail("个人简介不能为空");
        }
        
        // 创建职位申请对象
        JobApplication application = new JobApplication();
        application.setJobId(jobId);
        application.setName(name);
        application.setPhone(phone);
        application.setEmail(email);
        application.setIntroduction(introduction);
        
        // 处理简历文件
        if (resumeFile != null && !resumeFile.isEmpty()) {
            try {
                // 生成文件存储路径
                String dateDir = new SimpleDateFormat("yyyyMMdd").format(new Date());
                String filePath = uploadPath + "/resume/" + dateDir + "/";
                File dir = new File(filePath);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                
                // 生成唯一文件名
                String originalFilename = resumeFile.getOriginalFilename();
                String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
                String fileName = UUID.randomUUID().toString().replaceAll("-", "") + suffix;
                
                // 保存文件
                File destFile = new File(filePath + fileName);
                resumeFile.transferTo(destFile);
                
                // 设置简历文件路径
                application.setResumeFile("/resume/" + dateDir + "/" + fileName);
            } catch (IOException e) {
                e.printStackTrace();
                return Result.fail("简历文件上传失败");
            }
        }
        
        // 提交申请
        boolean success = jobApplicationService.submitApplication(application);
        
        if (success) {
            return Result.success();
        } else {
            return Result.fail("提交申请失败，请稍后重试");
        }
    }
    
    /**
     * 根据手机号查询职位申请
     * @param phone 手机号
     * @return 职位申请列表
     */
    @GetMapping("/applications/phone/{phone}")
    public Result<Object> getApplicationsByPhone(@PathVariable String phone) {
        if (phone == null || phone.trim().isEmpty()) {
            return Result.fail("手机号不能为空");
        }
        
        return Result.success(jobApplicationService.getApplicationsByPhone(phone));
    }
} 